//
//  CZVisitorView.swift
//  CZVVeiBo
//
//  Created by smile on 2017/7/30.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class CZVisitorView: UIView {
    
    // 01-声明闭包
    var closure:(()->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 监听事件
    @objc private func loginClick(){
        // 03-执行闭包
        closure?()
    }
    
    // MARK: - 供外界调用的方法
    func setupVisitorViewInfo(imgName: String?, title: String?){
        // 如果imgName&title都不为nil 代表的是消息发现我
        if let i = imgName, let t = title {
            // 设置图片
            logoImageView.image = UIImage(named:i)
            // 设置文字
            messageLabel.text = t
            // 隐藏圆圈图片
            circleImageView.isHidden = true
        }else{
            // 代表的是首页
            setupCircleImageViewAnimation()
        }
    }
    // MARK: - 设置圆圈动画
    private func setupCircleImageViewAnimation(){
        // 创建核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 设置动画执行时长
        anim.duration = 15
        // tovalue
        anim.toValue = M_PI * 2
        // 执行动画次数
        anim.repeatCount = MAXFLOAT
        // 如果切换控制器 或者 app退到后台 动画默认情况下会被移除
        anim.isRemovedOnCompletion = false
        // 添加动画
        circleImageView.layer.add(anim, forKey: nil)
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RGB(red: 235, green: 235, blue: 235)
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        addSubview(logoImageView)
        circleImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        maskImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.width.equalTo(230)
            make.top.equalTo(maskImageView.snp.bottom).offset(16)
            make.centerX.equalTo(self)
        }
        
        registerButton.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 100, height: 35))
            make.top.equalTo(messageLabel.snp.bottom).offset(16)
            make.left.equalTo(messageLabel)
        }
        
        loginButton.snp.makeConstraints { (make) in
//            make.top.equalTo(registerButton)
//            make.size.equalTo(registerButton)
            make.top.size.equalTo(registerButton)
            make.right.equalTo(messageLabel)
        }
        
        logoImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
//        UIImageView(imgName: "visitordiscover_feed_image_smallicon")
    }

    // MARK: - 懒加载控件
    // 圆圈图片
    private lazy var circleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 遮挡图片
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    // 文字
    private lazy var messageLabel: UILabel = {
        let lab = UILabel()
        // 字体大小
        lab.font = UIFont.systemFont(ofSize: 14)
        // 字体颜色
        lab.textColor = UIColor.darkGray
        // 文字
        lab.text = "关注一些人，回这里看看有什么惊喜"
        // 换行
        lab.numberOfLines = 0
        return lab
    }()
    // 注册按钮
    private lazy var registerButton: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(loginClick), for: UIControlEvents.touchUpInside)
        // 背景图片
        btn.setBackgroundImage(UIImage(named:"common_button_white"), for: UIControlState.normal)
        // title
        btn.setTitle("注册", for: UIControlState.normal)
        // title字体大小
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        // title字体颜色
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        btn.setTitleColor(CZAPPThemeColor, for: UIControlState.highlighted)
        return btn
    }()
    
    // 登录按钮
    private lazy var loginButton: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: #selector(loginClick), for: UIControlEvents.touchUpInside)
        // 背景图片
        btn.setBackgroundImage(UIImage(named:"common_button_white"), for: UIControlState.normal)
        // title
        btn.setTitle("登录", for: UIControlState.normal)
        // title字体大小
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        // title字体颜色
        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
        btn.setTitleColor(CZAPPThemeColor, for: UIControlState.highlighted)
        return btn
    }()
    
    // logo
    private lazy var logoImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_house")
}
